package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.service.ISysBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUnit;
import com.ruoyi.system.service.ISysUnitService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元信息Controller
 * 
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/system/unit")
public class SysUnitController extends BaseController
{
    private String prefix = "system/unit";

    @Autowired
    private ISysUnitService sysUnitService;

    @Autowired
    private ISysBuildingService sysBuildingService;

    @RequiresPermissions("system:unit:view")
    @GetMapping()
    public String unit()
    {
        return prefix + "/unit";
    }

    /**
     * 查询单元信息列表
     */
    @RequiresPermissions("system:unit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUnit sysUnit)
    {
        startPage();
        List<SysUnit> list = sysUnitService.selectSysUnitList(sysUnit);
        return getDataTable(list);
    }

    /**
     * 导出单元信息列表
     */
    @RequiresPermissions("system:unit:export")
    @Log(title = "单元信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUnit sysUnit)
    {
        List<SysUnit> list = sysUnitService.selectSysUnitList(sysUnit);
        ExcelUtil<SysUnit> util = new ExcelUtil<SysUnit>(SysUnit.class);
        return util.exportExcel(list, "unit");
    }

    /**
     * 新增单元信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("building", sysBuildingService.selectSysBuildingAll());
        return prefix + "/add";
    }

    /**
     * 新增保存单元信息
     */
    @RequiresPermissions("system:unit:add")
    @Log(title = "单元信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUnit sysUnit)
    {
        sysUnit.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysUnitService.insertSysUnit(sysUnit));
    }

    /**
     * 修改单元信息
     */
    @GetMapping("/edit/{unitId}")
    public String edit(@PathVariable("unitId") Long unitId, ModelMap mmap)
    {
        SysUnit sysUnit = sysUnitService.selectSysUnitById(unitId);
        mmap.put("building", sysBuildingService.selectSysBuildingByUnitId(unitId));
        mmap.put("sysUnit", sysUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存单元信息
     */
    @RequiresPermissions("system:unit:edit")
    @Log(title = "单元信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUnit sysUnit)
    {
        sysUnit.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysUnitService.updateSysUnit(sysUnit));
    }

    /**
     * 删除单元信息
     */
    @RequiresPermissions("system:unit:remove")
    @Log(title = "单元信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysUnitService.deleteSysUnitByIds(ids));
    }
}
